package com.jg.poiet.word;

import com.jg.poiet.exception.WordException;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.FileInputStream;

public abstract class NiceWord {

    public static NiceWord compile(String path) {
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(path);
            try {
                HWPFDocument document = new HWPFDocument(fis);
                return new HWPFNiceWord(document);
            } catch (Exception ignore) {
                try {
                    fis.close();
                } catch (Exception ignore1) {}
                fis = new FileInputStream(path);
                try {
                    XWPFDocument document = new XWPFDocument(fis);
                    return new XWPFNiceWord(document);
                } catch (Exception e) {
                    e.printStackTrace();
                    throw new WordException("暂不支持当前文件！");
                }
            }
        } catch (Exception e) {
            throw new WordException("文件不存在！");
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (Exception ignore) {}
            }
        }
    }

    /**
     * 转换成pdf文件
     * @param path pdf文件路径
     */
    public abstract void writeToPdfFile(String path);

}
